package org.xlx.xlx_oa.common;


import com.alibaba.fastjson.JSONObject;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.xlx.xlx_oa.SpringContextUtil;
import org.xlx.xlx_oa.role_user.domain.AdmUser;
import org.xlx.xlx_oa.utils.ThreadLocalUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Objects;

@Component
@Configuration
public class HeaderFilter implements HandlerInterceptor {
    private final AuthCache authCache = SpringContextUtil.getBean(AuthCache.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception {
        String token = request.getHeader(Constant.TOKEN);
        if (token == null) {
            response.reset();
            //设置编码格式
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter pw = response.getWriter();
//            throw new BusinessException("001","token is null");
            pw.write(JSONObject.toJSONString(ResponseBean.failureFactory("1000010", "token is null")));

            return false;
        }
        ThreadLocalUtil.set(Constant.TOKEN, token);
        AdmUser user = authCache.getSessionData(token);
        if (user == null) {
            response.reset();
            //设置编码格式
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter pw = response.getWriter();
//            throw new BusinessException("002","token is error");
            pw.write(JSONObject.toJSONString(ResponseBean.failureFactory("100009", "token is error")));
            return false;
        }
        ThreadLocalUtil.set(Constant.USERID, user.getId());
        ThreadLocalUtil.set(Constant.USERNAME, String.format("%s, %s%s", user.getLastName(), user.getMiddleName(), user.getFirstName()));
        ThreadLocalUtil.set(Constant.LEVEL, user.getLevel());
        ThreadLocalUtil.set(Constant.DEPARTMENTID, user.getDepartmentId());
        return true;
    }


    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o,
                           ModelAndView modelAndView) throws Exception {
        Object objToken = ThreadLocalUtil.get(Constant.TOKEN);
        String token = Objects.isNull(objToken) ? null : objToken.toString();
        AdmUser admUser = authCache.getSessionData(token);
        if (!Objects.isNull(admUser)) {
            authCache.expireSessionData(token);
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                                Object o, Exception e) throws Exception {

    }

}
